package com.sasis.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Properties;

public class PropertiesUtil {

	public static final String PASE_FLOW_SERVER = "FlowServer";
	public static final String PASE_RMI_PORT = "RMIPort";
	
	public static final String RT_URL_KEY = "RTURL";
	public static final String RT_LOGINNAME_KEY = "RTLoginName";
	public static final String RT_PASSWORD_KEY = "RTPassword";
	public static final String MSSQL_DRIVER_KEY = "MSSQLDriver";
	
	public static final String WS_SSOSOAP_ADDRESS = "WS_SSOSOAP_ADDRESS";
        public static final String HR_SSOSOAP_ADDRESS = "HR_SSOSOAP_ADDRESS";
	public static final String BMS_DOMAIN_NAME = "BMS_DOMAIN_NAME";
	public static final String BMS_LOGIN_URL = "BMS_LOGIN_URL";
	
	private Properties prop = new Properties();
	
	public PropertiesUtil(){
		initProperties("jeyo.properties");
	}
	
	public PropertiesUtil(String propertiesFileName){
		initProperties(propertiesFileName);
	}
	
	public PropertiesUtil(InputStream inputStream){
		initProperties(inputStream);
	}
	
	private void initProperties(String propertiesFileName){
		try{
			InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertiesFileName);
	        prop.load(inputStream);
	        if(inputStream != null){
	        	inputStream.close();
	        }
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	private void initProperties(InputStream inputStream){
		try{
	        prop.load(inputStream);
	        if(inputStream != null){
	        	inputStream.close();
	        }
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public String getValue(String keyName){
		try{
			String ret = prop.getProperty(keyName);
			if(ret == null) return "";
			return ret.trim();
		}catch(Exception ex){
			return "";
		}
	}
	/**
	 * function: modify value by key
	 * @param keyName
	 * @param toValue
	 * @param out
	 * @return ?
	 */
	public boolean modifyValue(String keyName,String toValue,OutputStream out){
		if(!prop.containsKey(keyName)){
			return false;
		}else{
			prop.setProperty(keyName, toValue);
			try {
				prop.store(out, keyName+"="+toValue);
				out.flush();
				out.close();
			} catch (IOException e) {
				return false;
			}
		}
		return true;
		
	}
	
	public URL getSSOSoapAddressURL(){
		try{
			String address = getValue(WS_SSOSOAP_ADDRESS);
			URL url = new URL(address);
			return url;
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
        
        
        public URL getHRSSOSoapAddressURL(){
		try{
			String address = getValue(HR_SSOSOAP_ADDRESS);
			URL url = new URL(address);
			return url;
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
        
        public static void main(String[] args) {
		try{
			PropertiesUtil propertiesUtil=new PropertiesUtil();
			System.out.println(propertiesUtil.getSSOSoapAddressURL());
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}


	
}
